﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static Work1_UI.Form1;

namespace Work1_UI
{
    class SelectFile
    {
        public SelectFile()
        {
            string[] strPhoFilt; //图片路径数组
            int pictureCout = 0; //记录一次性输入多少张图片

            FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
            folderBrowserDialog.Description = "请选择文件夹";
            if (folderBrowserDialog.ShowDialog() == DialogResult.OK || folderBrowserDialog.ShowDialog() == DialogResult.Yes)
            {
                //textBox1.Text = dilog.SelectedPath;
                strPhoFilt = Directory.GetFiles(folderBrowserDialog.SelectedPath);

                for (int i = 0; i < strPhoFilt.Length; i++)
                {
                    var ext = Path.GetExtension(strPhoFilt[i]); //获取后缀

                    if (ext == ".jpg" || ext == ".bmp" || ext == ".png") //判断是否是图片文件
                    {
                        pictureCout++; //记录输入了多少张图片
                        FileStream fileStream = new FileStream(strPhoFilt[i], FileMode.Open, FileAccess.Read); //使用流
                        Form1.form1.imageList1.Images.Add(Image.FromStream(fileStream)); //将获取到的图片添加至imageList
                        fileStream.Flush(); //清除缓冲区
                        fileStream.Close(); //关闭
                        fileStream.Dispose(); //释放
                    }
                }

                //使用委托添加图像
                Form1.form1.listView1.Invoke(new MethodInvoker(() =>
                {
                    Form1.form1.listView1.SmallImageList = Form1.form1.imageList1; //  SmallImageList 属性


                    for (int i = 0; i < pictureCout; i++) //显示imageList中新添加的图片
                    {
                        ListViewItem item = new ListViewItem();
                        item.ImageIndex = Form1.form1.imageList1.Images.Count - pictureCout + i;
                        item.Text = strPhoFilt[i]; //添加图片路径信息
                        Form1.form1.listView1.Items.Add(item);

                    }

                    Form1.form1.listView1.View = View.LargeIcon;
                    Form1.form1.listView1.LargeImageList = Form1.form1.imageList1;

                    pictureCout = 0; //复位                 

                    log.Info("读取文件夹");
                    form1.MessageShow(MessageType.Info, "读取文件夹");
                }));
            }
        }

    }
}
